Repository navigation
fix: preserve large exponents in recursive matrix powers - #2033
Open
lanqing431 wants to merge 1 commit into
Open
lanqing431 wants to merge 1 commit into
lanqing431 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe your change:
MatrixExponentiationRecursive([[1, 1], [0, 1]], 2 ** 31)overflows the call stack. The>> 1step converts the exponent to a signed 32-bit integer; the recursion eventually gets stuck at -1. At 2 ** 32 it silently returns the identity matrix instead of the correct power.Use
Math.floor(m / 2)to halve the exponent without truncating its bits. The six new cases use a triangular matrix whose exact power is[[1, n], [0, 1]], covering zero, small exponents, 2 ** 31, 2 ** 32 and Number.MAX_SAFE_INTEGER. They also check that the input matrix is unchanged.Validation (Node.js 24.22.0): the three large-exponent cases fail on unchanged master.
npm test -- --maxWorkers=2 --minWorkers=1passes all 11976 tests in 347 files.npm run check-styleandgit diff --checkpass.Checklist:
Prepared with OpenAI Codex assistance.